package com.cg.test;

/**
 * @author cheyinbo
 * @description 无锁有threadLocal 以空间换时间
 * @date 2021/8/4 9:47
 */
public class ThreadLocalDemo3 {

    private static ThreadLocal<String> threadLocal = new ThreadLocal<>();

    private String content;

    public String getContent() {
        return threadLocal.get();
    }
    public void setContent(String content) {
        threadLocal.set(content);
    }

    public static void main(String[] args) {
        ThreadLocalDemo3 threadLocalDemo3 = new ThreadLocalDemo3();
        for (int i = 0; i < 3; i++) {
            Thread thread = new Thread(() -> {
                threadLocalDemo3.setContent(Thread.currentThread().getName() + "的数据");
                System.out.println(Thread.currentThread().getName() + "->" + threadLocalDemo3.getContent());
            });
            thread.setName("线程" + i);
            thread.start();
        }
    }
}
